package eureka.ui.book;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import eureka.domain.book.BasicBookItem;
import eureka.domain.book.BookItem;
import eureka.domain.book.BookPicture;
import eureka.exceptions.DatabaseException;
import eureka.exceptions.ItemNotExistsException;
import eureka.service.book.IBookService;
import eureka.service.book.impl.BookService;

/**
 * Servlet implementation class GetBookPicture
 */
@WebServlet("/Books/GetBookPicture")
public class GetBookPicture extends HttpServlet {
	private static final long serialVersionUID = 1L;
     
	private IBookService bs = new BookService();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetBookPicture() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String strBbID = request.getParameter("bbid");
		String strBeID = request.getParameter("beid");
		String strRestrict = request.getParameter("restrict");
		String strIndex = request.getParameter("index");
		String strSize = request.getParameter("size");
		
		long beID = 0, bbID = 0;
		int index = 1;
		try {
			beID = Long.parseLong(strBeID);
		} catch (NumberFormatException e) {
			// Do Nothing
		}
		try {
			bbID = Long.parseLong(strBbID);
		} catch (NumberFormatException e) {
			// Do Nothing
		}
		try {
			index = Integer.parseInt(strIndex);
		} catch (NumberFormatException e) {
			// Do Nothing
		}
		
		if (bbID == 0 && beID == 0) {
			response.sendError(404);
			return;
		}
		
		boolean restrict = false;
		if (strRestrict != null && ("1".equals(strRestrict) || "true".equalsIgnoreCase(strRestrict)))
			restrict = true;
		
		boolean smallSize = false;
		if (strSize != null && "min".equalsIgnoreCase(strSize))
			smallSize = true;
		
		while (beID != 0) {
			BookItem be = null;
			try {
				be = bs.getBookByBeID(beID);
			} catch (DatabaseException e) {
				response.sendError(500);
				return;
			} catch (ItemNotExistsException e) {
				break;
			}
			if (be == null)
				break;
			bbID = be.getBbID();
			
			List<BookPicture> bps = null;
			try {
				bps = bs.getBookPicturesByBeID(beID);
			} catch (DatabaseException e) {
				response.sendError(500);
				return;
			}
			
			if (bps.size() == 0 && !restrict)
				break;
			
			if (index - 1 > bps.size()) {
				response.sendError(404);
				return;
			}
			
			BookPicture bp = bps.get(index - 1);
			
			if (smallSize)
				response.sendRedirect(request.getAttribute("userContents") + "/bepics/" + bp.getFileName() + ".min.jpg");
			else
				response.sendRedirect(request.getAttribute("userContents") + "/bepics/" + bp.getFileName() + "." + bp.getFileExt());
			
			return;
		}
		
		if (bbID == 0) {
			response.sendError(404);
			return;
		}
		
		BasicBookItem baiscbook = null;
		try {
			baiscbook = bs.getBasicBookByBbID(bbID);
		} catch (DatabaseException e) {
			response.sendError(500);
			return;
		}
		if (baiscbook == null) {
			response.sendError(404);
			return;
		}
		
		if (baiscbook.getDefaultPicture() == null || "".equals(baiscbook.getDefaultPicture())) {
			response.sendRedirect(request.getAttribute("staticResources") + "/image/bbpics_none.jpg");
			return;
		}
		
		if (smallSize)
			response.sendRedirect(request.getAttribute("userContents") + "/bbpics/" + baiscbook.getDefaultPicture() + ".min.jpg");
		else
			response.sendRedirect(request.getAttribute("userContents") + "/bbpics/" + baiscbook.getDefaultPicture());
		return;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
